package com.lee.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.lee.base.BaseService;
import com.lee.page.PageDomain;
import com.lee.page.PageResult;
import com.lee.system.api.IDictTypeService;
import com.lee.system.entity.DictType;
import com.lee.system.mapper.DictDataMapper;
import com.lee.system.mapper.DictTypeMapper;
import com.lee.utils.Convert;
import com.lee.utils.StringUtils;


/**
 * 字典 业务层处理
 * 
 * @author LiPengFei
 *
 */
@Service
public class DictTypeServiceImpl extends BaseService implements IDictTypeService {
	@Autowired
	private DictTypeMapper dictTypeMapper;

	@Autowired
	private DictDataMapper dictDataMapper;

	/**
	 * 根据条件查询字典类型
	 * 
	 * @param dictType
	 *            字典类型信息
	 * @return 字典类型集合信息
	 */
	@Override
	public List<DictType> selectDictTypeList(DictType dictType) {
		return dictTypeMapper.selectDictTypeList(dictType);
	}
	/**
	 * 根据条件分页查询字典类型
	 * 
	 * @param dictType
	 *            字典类型信息
	 * @return 字典类型集合信息
	 */
	@Override
	public PageResult selectDictTypeList(DictType dictType,PageDomain pageDomain) {
		startPage(pageDomain);
		Page<DictType> page = (Page<DictType>)dictTypeMapper.selectDictTypeList(dictType);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 根据所有字典类型
	 * 
	 * @return 字典类型集合信息
	 */
	@Override
	public List<DictType> selectDictTypeAll() {
		return dictTypeMapper.selectDictTypeAll();
	}

	/**
	 * 根据字典类型ID查询信息
	 * 
	 * @param dictId
	 *            字典类型ID
	 * @return 字典类型
	 */
	@Override
	public DictType selectDictTypeById(Long dictId) {
		return dictTypeMapper.selectDictTypeById(dictId);
	}

	/**
	 * 通过字典ID删除字典信息
	 * 
	 * @param dictId
	 *            字典ID
	 * @return 结果
	 */
	@Override
	public int deleteDictTypeById(Long dictId) {
		return dictTypeMapper.deleteDictTypeById(dictId);
	}

	/**
	 * 批量删除字典类型
	 * 
	 * @param ids
	 *            需要删除的数据
	 * @return 结果
	 */
	@Override
	public int deleteDictTypeByIds(String ids) throws Exception {
		Long[] dictIds = Convert.toLongArray(ids);
		for (Long dictId : dictIds) {
			DictType dictType = selectDictTypeById(dictId);
			if (dictDataMapper.countDictDataByType(dictType.getDictType()) > 0) {
				throw new Exception(String.format("%1$s已分配,不能删除", dictType.getDictName()));
			}
		}

		return dictTypeMapper.deleteDictTypeByIds(dictIds);
	}

	/**
	 * 保存字典类型信息
	 * 
	 * @param dictType
	 *            字典类型信息
	 * @return 结果
	 */
	@Override
	public int saveDictType(DictType dictType) {
		Long dictId = dictType.getDictId();
		if (StringUtils.isNotNull(dictId)) {
			return dictTypeMapper.updateDictType(dictType);
		} else {
			return dictTypeMapper.insertDictType(dictType);
		}
	}

	/**
	 * 校验字典类型称是否唯一
	 * 
	 * @param dictType
	 *            字典类型
	 * @return 结果
	 */
	@Override
	public boolean checkDictTypeUnique(DictType dict) {
		if (dict.getDictId() == null) {
			dict.setDictId(-1L);
		}
		DictType dictType = dictTypeMapper.checkDictTypeUnique(dict.getDictType());
		if (StringUtils.isNotNull(dictType) && dictType.getDictId().longValue() != dict.getDictId().longValue()) {
			return false;
		}
		return true;
	}
}
